
#include "kpar.h"
#include <qstring.h>
#include <kapplication.h>
#include <kaction.h>
#include <klocale.h>
#include <kactioncollection.h>
#include <kstandardaction.h>
#include <kfiledialog.h>
#include <kglobal.h>
#include <kio/netaccess.h>
#include <kiconloader.h>
#include <kconfigdialog.h>
#include "config.h"
#include "kparthread.h"
#include "kpar2customevents.h"


Kpar::Kpar(QWidget *parent) : KXmlGuiWindow(parent) {

    setupActions();
    setupGUI();
    ui.setupUi(this);
    thr = new KparThread( this );
    ui.file_progress->setValue(0);
    ui.total_progress->setValue(0);
    KIconLoader kl;
    p_info = kl.loadIcon("documentinfo.png",KIconLoader::NoGroup );
    p_ok = kl.loadIcon("dialog-ok-apply.png", KIconLoader::NoGroup);
    p_damaged = kl.loadIcon("dialog-cancel.png", KIconLoader::NoGroup);
    p_error = kl.loadIcon("edit-delete.png",KIconLoader::NoGroup);
    p_warning = kl.loadIcon("edit-delete.png",KIconLoader::NoGroup );
}

Kpar::~Kpar()
{
    thr->terminate();
}

void Kpar::setupActions()
{
  KStandardAction::quit(kapp, SLOT(quit()),actionCollection());
  KStandardAction::open(this, SLOT(openFile()),actionCollection());
  KStandardAction::preferences(this, SLOT(slotConfig()),actionCollection());
}

void Kpar::slotConfig()
{
    if(KConfigDialog::showDialog(i18n("Settings")))
            return;

    KConfigDialog *dialog = new KConfigDialog(this, i18n("Settings"), Config::self());
    dialog->showButtonSeparator( true );

    // general settings
    General *general = new General(this);
    dialog->addPage(general, i18n("General"), "kpar2", i18n("General Settings"));
    connect(dialog, SIGNAL(settingsChanged(const QString &)), SLOT(updateSettings()));
    dialog->show();

}

void Kpar::openFile()
{
    QString fileNameFromDialog = KFileDialog::getOpenFileName(KUrl(), ".*PAR *.PAR2",this, i18n("Open Par2 file"));
    if(KIO::NetAccess::download(fileNameFromDialog, file_name,this))
    {
        clear_gui();
        thr->start_process(file_name);
    }
}

void Kpar::openURL(KUrl url)
{
    url.cleanPath();
    clear_gui();
    file_name = KIO::NetAccess::mostLocalUrl( url, this ).path();
    QFile file(file_name);
    thr->start_process(file.fileName());
}

void Kpar::clear_gui()
{
     ui.total_progress->setValue(0);
     ui.file_progress->setValue(0);
     ui.listWidget_File->clear();
     ui.check_Button->setEnabled(false);
     ui.repair_Button->setEnabled(false);
}

void Kpar::changeEvent(QEvent *e)
{
    QMainWindow::changeEvent(e);
    switch (e->type()) {
    case QEvent::LanguageChange:
        ui.retranslateUi(this);
        break;
    default:
        break;
    }
}

void Kpar::on_check_Button_clicked()
{
    if (file_name.trimmed() != "")
    {
        ui.check_Button->setEnabled(false);
        ui.repair_Button->setEnabled(false);
        thr->start_process_check(file_name);
    }
}

void Kpar::on_repair_Button_clicked()
{
    if (file_name.trimmed() != "")
    {
        ui.check_Button->setEnabled(false);
        ui.repair_Button->setEnabled(false);
        thr->start_process_repair(file_name);
    }
}
void Kpar::customEvent( QEvent *e )
{
    if (e->type() == QEvent::Type(QEvent::User + 1 ) )
    {
        UpdateProgress* up = ( UpdateProgress* )e;
        if (up->type() == file )
        {
            ui.file_progress->setValue(up->progress());
        }
        else
        {
            ui.total_progress->setValue(up->progress());
        }
    } else if  (e->type() == QEvent::Type(QEvent::User + 2 ) )
    {
        UpdateListe* ul = ( UpdateListe* )e;
        QPixmap i;
        switch( ul->status() ){
        case info:
            i = p_info;
            break;
        case ok:
            i = p_ok;
            break;
        case warning:
            i = p_warning;
            break;
        case error:
            i = p_error;
            break;
        default:
            break;
        }
        QListWidgetItem* item = new QListWidgetItem(i, ul->text());
        ui.listWidget_File->addItem(item);
        ui.listWidget_File->scrollToItem(item);
    }else if  (e->type() == QEvent::Type(QEvent::User + 3 ) )
    {
        UpdateStatusBar* us = ( UpdateStatusBar* )e;
        ui.statusBar->showMessage(us->statusbar());
    }
}
